package com.leinao.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author  wangshoufa 
 * @date 2018年10月9日 下午2:06:55
 *
 */

@Configuration
@EnableSwagger2
@Profile({"dev"})

public class SwaggerPluginConfig {

    @Autowired
    private Environment env;

    @Bean
    public Docket createRestApi() {
    	ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();  
        tokenPar.name("token").description("user token")
    	.modelRef(new ModelRef("string")).parameterType("header") 
    	.required(false).build(); //header中的ticket参数非必填，传空也可以
    	pars.add(tokenPar.build());
    	
    	ParameterBuilder bitaTokenPar = new ParameterBuilder();
    	bitaTokenPar.name("bitaToken").description("user bitaToken")
    	.modelRef(new ModelRef("string")).parameterType("header") 
    	.required(false).build(); //header中的ticket参数非必填，传空也可以
    	pars.add(bitaTokenPar.build());
    	
        ApiInfo apiInfo = new ApiInfoBuilder().title(env.getProperty("spring.application.name") + " API").contact("PAI SERVICE WEB")
                .version("1.0.RELEASE").build();
        return new Docket(DocumentationType.SWAGGER_2).
        		apiInfo(apiInfo).select()
                .apis(RequestHandlerSelectors.basePackage("com.leinao"))
                .paths(PathSelectors.any()).build()
                .globalOperationParameters(pars);
    }
    
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot中使用Swagger2构建RESTful APIs")
                .description("更多Spring Boot相关文章请关注：https://gitee.com/damon_one")
                .termsOfServiceUrl("https://gitee.com/damon_one")
                .contact("Damon")
                .version("1.0")
                .build();
    }
}
